<template>
  <div>
    <el-input v-model="filterText" placeholder="输入关键字进行过滤" />

    <el-tree
      ref="tree"
      :data="treeData"
      :props="defaultProps"
      :filter-node-method="filterNode"
      highlight-current
      class="filter-tree"
      node-key="id"
      default-expand-all
      :expand-on-click-node="false"
      :default-checked-keys="defaultCheckKey"
      @node-click="handleNodeClick"
    />
  </div>

</template>
<script>
// import $http from '@/api/server/http'
// import orgData from '@/api/orgData.js'
export default {
  name: 'ZlstTree',
  model: {
    prop: 'value',
    event: 'change'
  },
  data() {
    return {
      filterText: '',
      // treeData: [],
      defaultProps: {
        children: 'subGroups',
        label: 'name'
      },
      defaultCheckKey: []
    }
  },
  computed: {
    treeData() {
      if (this.$store.state.orgData.data) {
        return this.$store.state.orgData.data
      }
      return []
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getTreeData()
  },
  methods: {
    getTreeData() {
      const args = ['organizations', {}, {
        headers: {
          Authorization: 'Bearer ' + this.$store.state.user.token,
          clientId: this.$store.state.user.clientId,
          realm: this.$store.state.user.realm
        }
      }]
      this.$store.dispatch('orgData/getOrg', args)
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    // 点击树节点获取节点详情
    handleNodeClick(data) {
      var id = ''
      // 点击节点传入数据格式和直接调用传入的数据格式不一样需要判断来取值
      if (typeof data === 'object') {
        id = data.id
      } else {
        id = data
      }
      this.$emit('clickTreeRow', id)
    }
  }
}
</script>
